
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Seccont;


public class GenericSeccontDAO extends AutoKeys implements org.jetnuke.dao.SeccontDAO {


	/* (non-Javadoc)
	 * @see org.jetnuke.model.SeccontModel#retrieveByPK(int)
	 */
	public Seccont retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SeccontModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		Seccont s = new Seccont();
		
		if (rs.next()) {
			fillSeccontFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillSeccontFromResultSet(Seccont s, ResultSet rs)throws Exception {
		s.setARTID(rs.getInt("ARTID"));
		s.setCONTENT(rs.getString("CONTENT"));
		s.setCOUNTER(rs.getInt("COUNTER"));
		s.setSECID(rs.getInt("SECID"));
		s.setSLANGUAGE(rs.getString("SLANGUAGE"));
		s.setTITLE(rs.getString("TITLE"));
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.SeccontModel#update(org.jetnuke.entities.Seccont)
	 */
	public void update(Seccont seccont) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("SeccontModel.update");
		p.setString(1,seccont.getCONTENT());
		p.setString(2,seccont.getSLANGUAGE());
		p.setString(3,seccont.getTITLE());
		p.setInt(4,seccont.getCOUNTER());
		p.setInt(5,seccont.getSECID());
		p.setInt(6,seccont.getARTID());
		p.executeUpdate();
		p.close();
		
	}
	private void initNewSeccont(Seccont seccont, PreparedStatement p)  throws Exception{
		p.setString(1,seccont.getCONTENT());
		p.setString(2,seccont.getSLANGUAGE());
		p.setString(3,seccont.getTITLE());
		p.setInt(4,seccont.getCOUNTER());
		p.setInt(5,seccont.getSECID());
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.SeccontModel#incCounter(int)
	 */
	public void incCounter(int it) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SeccontModel.incCounter"));
		p.setInt(1, it);
		
		p.executeUpdate();
		p.close();
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.SeccontModel#addNew(org.jetnuke.entities.Seccont)
	 */
	public int addNew(Seccont article) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("SeccontModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("SeccontModel.lastGeneratedSeccontID"));
		initNewSeccont(article, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.SeccontModel#selectAllLimit(int)
	 */
	public List selectAllLimit(int i) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SeccontModel.selectAllLimit"));
		p.setInt(1,i);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	protected List processSelectAll(ResultSet rs) throws Exception  {
		List list = new ArrayList();
		while (rs.next()) {
			Seccont u = new Seccont();
			fillSeccontFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.SeccontModel#selectAllBySECID(int)
	 */
	public List selectAllBySECID(int secid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SeccontModel.selectAllBySECID"));
		p.setInt(1,secid);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.SeccontModel#searchOrderArtidDesc(java.lang.String)
	 */
	public List searchOrderArtidDesc(String query) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SeccontModel.searchOrderArtidDesc"));
		p.setString(1,query);	
		p.setString(2,query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.SeccontModel#selectOrderCounterDesc(int)
	 */
	public List selectOrderCounterDesc(int limit) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SeccontModel.selectOrderCounterDesc"));
		p.setInt(1,limit);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}	
}
